<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScroll from '@better-scroll/core'
import ObserveDom from '@better-scroll/observe-dom'
import ObserveImage from '@better-scroll/observe-image'
import PullUp from '@better-scroll/pull-up'
BScroll.use(PullUp)
BScroll.use(ObserveDom)
BScroll.use(ObserveImage)
export default {
  name: "Scroll",
  props: {
    probeType: {
      type: Number,
      default: 0
    },
    pullUpLoad: {
      type: Boolean,
      default: false
    }
  },
  data(){
    return {
      scroll: null,
    }
  },
  mounted() {
    //创建BScroll对象
    this.scroll = new BScroll(this.$refs.wrapper,{
      ObserveDom: true,
      ObserveImage: true,
      click: true,
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad
    })
    //监听滚动的位置
    if(this.probeType === 2 || this.probeType ===3){
      this.scroll.on('scroll', (position) => {
        this.$emit('scroll', position)
      })
    }
    //监听scroll滚动到底部并上拉
    if(this.pullUpLoad){
      this.scroll.on('pullingUp', () => {
        this.$emit('pullingUp')
      })
    }
    // this.scroll.refresh()
  },
  methods:{
    scrollTo(x, y, time=300){
      this.scroll && this.scroll.scrollTo(x, y, time)
    },
    finishPullUp(){
      this.scroll && this.scroll.finishPullUp()
    },
    refresh(){
      this.scroll.refresh()
    }
  }
}
</script>

<style scoped>

</style>
